package com.kevin.redisson.configuration;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import java.util.Random;

@Getter
@Setter
@ToString
@Accessors(chain = true)
@ConfigurationProperties(prefix = "server")
public class SnowflakeProperties implements Serializable {

    @Serial
    private static final long serialVersionUID = 8716673459420965846L;

    private static final int MAX_VALUE = 32;

    private Integer datacenterId;

    private Integer machineId;

    public Integer getDatacenterId() {
        return (Objects.isNull(datacenterId) || datacenterId > MAX_VALUE) ? new Random().nextInt(MAX_VALUE) : datacenterId;
    }

    public Integer getMachineId() {
        return (Objects.isNull(machineId) || machineId > MAX_VALUE) ? new Random().nextInt(MAX_VALUE) : machineId;
    }

}
